<template>
	<view class="index">
		<!-- <nav-bar isBack backColor="#fff" background="transparent" isPlace="false"></nav-bar> -->
		<nav-bar isBack background="transparent" isPlace="false"></nav-bar>
		
		<!-- 顶部 -->
		<view class="i_top">
			<view class="i_tBg"><image src="https://manager.ryz1620.com/static/enter/bg_6.png"></image></view>
			<view class="i_tText"><view class="i_tIcon"><image src="https://manager.ryz1620.com/static/enter/icon_5.png"></image></view><text>入驻申请</text></view>
		</view>
		
		<!-- 内容 -->
		<view class="i_container">
			<view >
				<view class="i_cItem" @tap="toApply('shop')">
					<view class="i_cIcon icon1"><image src="https://manager.ryz1620.com/static/enter/icon_1.png"></image></view>
					<view class="i_cName">家居建材商</view>
					<view class="i_cRemark">给人们设计一个温暖的家</view>
				</view>
				<!-- <view class="i_cItem" @tap="toApply('designer')">
					<view class="i_cIcon icon2"><image src="https://manager.ryz1620.com/static/enter/icon_2.png"></image></view>
					<view class="i_cName">装饰公司</view>
					<view class="i_cRemark">给人们设计一个温暖的家</view>
				</view> -->
				<view class="i_cItem" @tap="toApply('decoration')">
					<view class="i_cIcon icon2"><image src="https://manager.ryz1620.com/static/enter/icon_2.png"></image></view>
					<view class="i_cName">装修公司</view>
					<view class="i_cRemark">给人们设计一个温暖的家</view>
				</view>
				<view class="i_cItem" @tap="toApply('builder')">
					<view class="i_cIcon icon3"><image src="https://manager.ryz1620.com/static/enter/icon_3.png"></image></view>
					<view class="i_cName">施工团队</view>
					<view class="i_cRemark">给人们设计一个温暖的家</view>
				</view>
				<view class="i_cItem" @tap="toApply('designer_per')">
					<view class="i_cIcon icon4"><image src="https://manager.ryz1620.com/static/enter/icon_4.png"></image></view>
					<view class="i_cName">设计师工作室</view>
					<view class="i_cRemark">给人们设计一个温暖的家</view>
				</view>
				<view class="i_cItem" @tap="toApply('maker')">
					<view class="i_cIcon icon4"><image src="https://manager.ryz1620.com/static/enter/icon_6.png"></image></view>
					<view class="i_cName">创客机构</view>
					<view class="i_cRemark">给人们设计一个温暖的家</view>
				</view>
			</view>
		</view>
		
	</view>
</template>

<script>
import navBar from "@/components/navBar/navBar.vue"
import service_enter from '@/service/enter.js'

export default {
	data() {
		return {
			// 登录信息
			token: null,           // token
			isBindMobile: null,    // 是否绑定手机号
			userMobile: null,      // 用户手机号
			// 审核信息
			enterGet: 'null',      // 是否获取到用户审核信息   init: 初始用户  nall: 获取失败 
			enterStatus: null,     // 审核状态    null：未申请  0：申请中  1：申请通过  2：申请不通过
			enterRole: null,       // 入驻角色，申请成功enterStatus=2    designer：设计师  shop：品牌商  builder：施工方  guide：导购
			enterCheck: null,      // PC登录信息查看状态，申请成功enterStatus=2    0：未查看  1：已查看
			enterReason: null,
			enterType:null
		}
	},
	
	computed: {
		isAnewApply() { return this.$store.state.index.isAnewApply },
		isAnewSubmit() { return this.$store.state.index.isAnewSubmit },
	},
	
	components: { navBar },
	
	onLoad() {
		this.getEnterStatus()
		this.getToken()
		this.toApply()
	},
	onUnload() {
	    // uni.reLaunch({
	    //   url: '/pages/mall/newIndex/newIndex'
	    // })
	 },
	onShow() {
		if (this.isAnewSubmit) {
			this.enterGet = 'null'
			this.$store.commit("index/SET_isAnewSubmit", false)
		}
	},
	
	methods: {
		// 获取token
		getToken({ isGetEnter = false, callback } ={}) {
			let token = this.$store.state.index.token
			let tokenTime = this.$store.state.index.tokenTime
			// 2小时以内
			if (token !='') {  // 7200000: 2小时
				this.token = token
				this.isBindMobile = this.$store.state.index.isBindMobile
				this.userMobile = this.$store.state.index.userMobile
			}
			// 2小时以后 => 重新获取token
			else {
               uni.navigateTo({
               	url: '/pages_login/pages/login/login/index',
               });
			}
		},
		
		// 获取入驻状态
		getEnterStatus({ isAgain = false, callback } = {}) {
			if (isAgain) this.$utils.showLoading()
			service_enter.getEnterStatus({})
				.then(res => {
					this.$utils.hideLoading()
					console.log(res,'入驻状态')
					if (res.code == 1) {
						let data = res.data
						this.enterGet = 'get'
						this.enterStatus = data.status    // 申请状态
						this.enterRole = data.role        // 入驻角色
						this.enterCheck = data.check_psw  // PC登录信息查看状态
						this.enterReason = data.reason 
						this.enterType = data.type //入驻情况
						
						if (typeof callback === 'function') callback()
					}
					else {
						this.$utils.showErr({ title: res.msg })
					}
				})
				.catch(err => {
					this.$utils.hideLoading()
					this.$utils.showErr()
				})
		},
		
		// 所有权限
		getAllRoot(){
			let params = {
				user_id:this.$store.state.index.userId,
				page:1,
				pagesize:10
			}
			this.$api.allRoot(params).then(res=>{
				console.log(res,'所有权限')
				if(res.code==1){
					let page = res.data
					this.$store.commit('index/SET_page', page)
				}
			})
		},
		
		// 跳转页面
		toApply(page) {
			console.log(this.enterGet,'fffff')
			let token = this.token
			let enterGet = this.enterGet
			let enterStatus = this.enterStatus
			let isAnewApply = this.isAnewApply
			if (token) {
				// 初始用户 => 跳转申请页面
				if (enterGet === 'init') {
					this.toPage(page)
				} 
				// 重新提交审核 => 跳转申请页面
				else if (isAnewApply) {
					this.toPage(page)
				}
				else if (enterGet === 'get') {
					// 申请中
					if (enterStatus === 0) {
						this.enterGet = 'null'   // 下次重新获取审核状态
						uni.navigateTo({ url: "/pages/enter/index/audit" })
						if(this.enterType=='2'){
							uni.navigateTo({
								url:'./audit'
							})
						}
					}
					// 申请通过
					else if (enterStatus === 1) {
						this.getAllRoot()
						// uni.navigateTo({ url: "/pages/enter/index/success"})
						uni.showToast({
							title:'申请已通过，重新加载中...',
							icon:'none'
						})
						setTimeout(function(){
							uni.reLaunch({
								url:'/pages/mall/newIndex/newIndex'
							})
						},1500)
					}
					// 申请失败
					else if (enterStatus === 2) {
						uni.navigateTo({ url: "/pages/enter/index/fail?reason=" + this.enterReason })
					}
					else {
						this.enterGet = 'null'   // 下次重新获取审核状态
						this.toPage(page)
					}
				}
				// 重新获取审核状态
				else {
					this.getEnterStatus({ isAgain: true, callback: () => this.toApply(page) })
				}
			}
			// 重新获取token
			else {
				this.getToken({ isGetEnter: true, callback: () => this.toApply(page) })
			}
		},
		
		// 跳转页面
		toPage(page) {
			if (page === 'shop') uni.navigateTo({ url: "/pages/enter/shop/shop" })
			else if (page === 'designer') uni.navigateTo({ url: "/pages/enter/designer/designer" })
			else if (page === 'decoration') uni.navigateTo({ url: "/pages/enter/decoration/decoration" })
			else if (page === 'builder') uni.navigateTo({ url: "/pages/enter/builder/builder" })
			else if (page === 'designer_per') uni.navigateTo({ url: "/pages/enter/designer_per/designer_per" })
			else if (page === 'maker') uni.navigateTo({ url: "/pages/enter/maker/maker" })
		}
	}
}
</script>

<style lang="scss" scoped>
.index {width: 100vw;height: 100vh;background: #fff;
	// 顶部
	.i_top {height: 400rpx;position: relative;
		.i_tBg {height: 100%;background: #ccc;filter: blur(1px);}
		.i_tText {width: 100%;height: 80rpx;color: #fff;font-size: 56rpx;position: absolute;top: 222rpx;left: 0;display: flex;justify-content: center;align-items: center;}
		.i_tIcon {width: 60rpx;height: 52rpx;margin-right: 10rpx;display: inline-block;}
	}
	
	// 内容
	.i_container {padding-top: 82rpx;margin-top: -22rpx;border-radius: 24rpx 24rpx 0 0;background: #fff;position: relative;overflow: hidden;
		.i_cItem {width: 598rpx;height: 180rpx;text-align: left;padding-top: 42rpx;padding-left: 244rpx;margin: 0 auto 80rpx;border-radius: 16rpx;background: #f8f6f3;position: relative;box-shadow: 0 4rpx 12rpx 0 rgba(0, 0, 0, .1);}
		.i_cName {height: 50rpx;line-height: 50rpx;color: #1a1c20;font-size: 36rpx;}
		.i_cRemark {height: 34px;line-height: 34rpx;color: #939393;font-size: 24rpx;margin-top: 12rpx;}
		.i_cIcon {width: 244rpx;height: 180rpx;position: absolute;top: 0;left: 0;display: flex;justify-content: center;align-items: center;
			&.icon1 > image {width: 116rpx;height: 116rpx;}
			&.icon2 > image {width: 108rpx;height: 108rpx;}
			&.icon3 > image {width: 128rpx;height: 112rpx;}
			&.icon4 > image {width: 98rpx;height: 98rpx;}
		}
	}
}
</style>